<?php
require_once(CORE_DIR.'Common.php');
require_once(CORE_DIR.'mappers/LevelMapper.php');

class levelModel extends Model{
	
	function __construct(){
		$this->conn = $this->getConnection();
	}
	
	function addLevel($level, $pointTypeId, $points, $title, $imagePath){
		$query = "insert into level (level, pointTypeId, points, title, imagePath)
				  values (%d, %d, %d, '%s', '%s')";
		$query = $this->format($query, array($level, $pointTypeId, $points, $title, $imagePath));
		return $this->execute($query, null);			
	}	
	
	function updateLevelByPointTypeId($level, $pointTypeId, $points, $title, $imagePath){
		$query = "update level set title='%s', imagePath='%s', points=%d where level=%d and pointTypeId=%d";
		$query = $this->format($query, array($points, $title, $imagePath, $level, $pointTypeId));		
		return $this->execute($query, null);	
	}

	function deleteLevel($level){
		$query = "delete from level where level=%d";
		$query = $this->format($query, array($level));		
		return $this->execute($query, null);
	}		
	
	function deleteLevelByPointTypeId($level, $pointTypeId){
		$query = "delete from level where level=%d and pointTypeId=%d";
		$query = $this->format($query, array($level, $pointTypeId));		
		return $this->execute($query, null);
	}		
	
	function getAllLevels(){
		$query = "SELECT * FROM level";		
		return $this->execute($query, new LevelMapper());		
	}
	
	function getLevelByPointTypeId($level, $pointTypeId){
		$query = "SELECT * FROM level WHERE level=%d and pointTypeId=%d";
		$query = $this->format($query, array($level, $pointTypeId));		
		return $this->execute($query, new LevelMapper());
	}	
	
	function getLevelById($level){
		$query = "SELECT * FROM level WHERE level=%d";
		$query = $this->format($query, array($level));		
		return $this->execute($query, new LevelMapper());
	}	
}
?>
